package de.openkanban.client.ui.board;

import java.util.ArrayList;
import java.util.List;

import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import de.openkanban.client.login.LogIn;
import de.openkanban.client.ui.presenter.GetActivitiesByBoardIdRequestCallback;
import de.openkanban.client.ui.presenter.GetBoardByIdRequestCallback;
import de.openkanban.client.ui.presenter.GetDefaultBoardByUserIdRequestCallback;
import de.openkanban.client.ui.presenter.PreparedRequest;
import de.openkanban.client.util.RootPanel;
import de.openkanban.shared.dto.ActivityDto;
import de.openkanban.shared.dto.BoardDto;

/**
 * GUI Element for the WhiteBoardView. Part of the Board Composite. Should react
 * if board switches.
 */
public class WhiteBoardView extends VLayout implements BoardComposite {

	private List<ActivityView> activityViewList;
	private HLayout activitiesHLayout;
	private BoardDto boardDto;
	private EnterBoardNameView enterBoardNameView;

	public WhiteBoardView() {
		activityViewList = new ArrayList<ActivityView>();

		activitiesHLayout = new HLayout();
		activitiesHLayout.setHeight100();

		setShowEdges(true);
		setMargin(5);
		setEdgeImage("corners/wb3.png");
		setEdgeSize(23);
		setPadding(5);
		setHeight100();
		setBackgroundColor("#eeeeee");
		setOverflow(Overflow.AUTO);
		setVisible(true);

		addMember(activitiesHLayout);
		RootPanel.boardComponent.addComposite(this);

		loadDefaultBoard();
	}

	public void loadDefaultBoard() {
		PreparedRequest prepReq = new PreparedRequest("board", "user/" + LogIn.getUserDto().getUserId().toString());
		prepReq.sendAsyncRequest(new GetDefaultBoardByUserIdRequestCallback());
	}

	public void loadActivities(BoardDto boardDto) {
		setBoardDto(boardDto);
		loadActivities();
	}

	public void loadActivities() {
		if (boardDto.getBoardId() != null) {
			PreparedRequest prepRequest = new PreparedRequest("board", boardDto.getBoardId().toString() + "/activity/all");
			prepRequest.sendAsyncRequest(new GetActivitiesByBoardIdRequestCallback(this));
		}
	}

	@Override
	public void switchBoard(Long boardId) {
		removeActivitiesHLayoutMembers();
		activityViewList.clear();
		PreparedRequest prepRequest = new PreparedRequest("board", boardId.toString());
		prepRequest.sendAsyncRequest(new GetBoardByIdRequestCallback());
	}

	public void addActivity(ActivityView activityView) {
		if (activityView.getActivityDto() == null || !activityExists(activityView.getActivityDto().getActivityId())) {
			activitiesHLayout.addMember(activityView);
			activityViewList.add(activityView);
			if (activityViewList.size() == 1) {
				RootPanel.main.getBoard().showNewKanbanButton();
			}
		}
	}

	public void removeActivity(ActivityView activityView) {
		activitiesHLayout.removeMember(activityView);
		activityViewList.remove(activityView);
	}

	public void removeActivitiesHLayoutMembers() {
		Canvas[] members = activitiesHLayout.getMembers();
		activitiesHLayout.removeMembers(members);
		activityViewList.clear();
	}

	public boolean hasActivity() {
		return activityViewList.size() > 0;
	}

	public ActivityDto getFirstActicityDto() {
		Long min = Long.MAX_VALUE;
		ActivityDto minActvity = new ActivityDto();
		for (ActivityView activityView : activityViewList) {
			ActivityDto activityDto = activityView.getActivityDto();
			if (activityDto != null) {
				if (activityDto.getActivityId() < min) {
					min = activityDto.getActivityId();
					minActvity = activityDto;
				}
			}
		}
		return minActvity;
	}

	private boolean activityExists(Long activityId) {
		for (ActivityView activityView : activityViewList) {
			if (activityView.getActivityDto().getActivityId() == activityId) {
				return true;
			}
		}
		return false;
	}

	public void showEnterBoardNameView() {
		enterBoardNameView = new EnterBoardNameView();
		hideAllActivities();
		activitiesHLayout.addMember(enterBoardNameView);
	}

	public void hideEnterBoardNameView() {
		if (enterBoardNameView != null) {
			activitiesHLayout.removeMember(enterBoardNameView);
		}
		showAllActivites();
		if (activityViewList.size() > 0) {
			RootPanel.main.getBoard().showNewKanbanButton();
		}
	}

	private void showAllActivites() {
		for (ActivityView activityView : activityViewList) {
			activityView.show();
		}
	}

	private void hideAllActivities() {
		for (ActivityView activityView : activityViewList) {
			activityView.hide();
		}
	}

	public BoardDto getBoardDto() {
		if (boardDto == null) {
			boardDto = new BoardDto();
		}
		return boardDto;
	}

	public void setBoardDto(BoardDto boardDto) {
		this.boardDto = boardDto;
	}

	public HLayout getActivitiesHLayout() {
		return activitiesHLayout;
	}

	public List<ActivityView> getActivityViewList() {
		return activityViewList;
	}

	public ActivityView getActivityViewById(Long activityId) {
		for (ActivityView activityView : activityViewList) {
			if (activityView.getActivityDto().getActivityId() == activityId) {
				return activityView;
			}
		}

		return null;
	}

}
